package com.sunzr0119.conventioncentertemplatebackend.controller;

import com.sunzr0119.conventioncentertemplatebackend.dto.NoticeDTO;
import com.sunzr0119.conventioncentertemplatebackend.entity.Notice;
import com.sunzr0119.conventioncentertemplatebackend.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/notice-api")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @GetMapping("/getAllNotices")
    public ResponseEntity<List<Notice>> getAllNotices() {
        return noticeService.getNoticeList();
    }

    @PostMapping("/uploadNotice")
    public ResponseEntity<String> uploadNotice(@RequestParam("file") MultipartFile file,
                                               @RequestParam("uploaderId") String uploaderId,
                                               @RequestParam("title") String title,
                                               @RequestParam("description") String description) {
        NoticeDTO noticeDTO = new NoticeDTO();
        noticeDTO.setTitle(title);
        noticeDTO.setDescription(description);
        noticeDTO.setUploaderId(uploaderId);
        return noticeService.uploadNotice(file, noticeDTO);
    }

    @PostMapping("/delete/{id}")
    public ResponseEntity<String> deleteNotice(@PathVariable String id) {
        return noticeService.deleteNotice(id);
    }

    @GetMapping("/download/{id}")
    public ResponseEntity<Resource> downloadNotice(@PathVariable String id) {
        return noticeService.downloadNotice(id);
    }
}
